package org.blame.lotterysystem.service.activityStatus.operator;

import org.blame.lotterysystem.dao.dataObject.ActivityUserDO;
import org.blame.lotterysystem.dao.mapper.ActivityUserMapper;
import org.blame.lotterysystem.service.dto.ConvertActivityStatusDTO;
import org.blame.lotterysystem.service.enums.ActivityUserEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
public class UserOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityUserMapper activityUserMapper;
    @Override
    public Integer sequence() {
        return 0;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        ActivityUserEnum targetUserStatus = convertActivityStatusDTO.getTargetUserStatus();
        if(activityId==null || CollectionUtils.isEmpty(userIds) ||targetUserStatus==null){
            return false;
        }
        List<ActivityUserDO> activityUserDOS = activityUserMapper.batchSelectByAUIds(activityId, userIds);
        if (CollectionUtils.isEmpty(activityUserDOS)){
            return false;
        }

        for(ActivityUserDO auDO:activityUserDOS){
            if(targetUserStatus.name().equalsIgnoreCase(auDO.getStatus())){
                return false;
            }
        }
        return true;
    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        List<Long> userIds = convertActivityStatusDTO.getUserIds();
        ActivityUserEnum targetUserStatus = convertActivityStatusDTO.getTargetUserStatus();
        try {
            activityUserMapper.batchUpdateStatus(activityId,userIds,targetUserStatus.name());
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
